package org.dromara.fms.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.web.core.BaseService;
import org.dromara.fms.domain.FmsBill;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.fms.service.impl.FmsBillServiceImpl;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 财务单据
 *
 * @author wenrui Su
 * @date 2024-03-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/fms/bill")
public class FmsBillController extends BaseService {

    private final FmsBillServiceImpl fmsBillService;

    /**
     * 查询财务单据列表
     */
    @SaCheckPermission("fms:bill:list")
    @GetMapping("/list")
    public TableDataInfo<FmsBill> list(FmsBill bo, PageQuery pageQuery) {
        return fmsBillService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出财务单据列表
     */
    @SaCheckPermission("fms:bill:export")
    @Log(title = "财务单据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(FmsBill bo, HttpServletResponse response) {
        List<FmsBill> list = fmsBillService.queryList(bo);
        ExcelUtil.exportExcel(list, "财务单据", FmsBill.class, response);
    }

    /**
     * 获取财务单据详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("fms:bill:query")
    @GetMapping("/{id}")
    public R<FmsBill> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(fmsBillService.queryById(id));
    }

    /**
     * 新增财务单据
     */
    @SaCheckPermission("fms:bill:add")
    @Log(title = "财务单据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody FmsBill bo) {
        return toAjax(fmsBillService.insertByBo(bo));
    }

    /**
     * 修改财务单据
     */
    @SaCheckPermission("fms:bill:edit")
    @Log(title = "财务单据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody FmsBill bo) {
        return toAjax(fmsBillService.updateByBo(bo));
    }

    /**
     * 删除财务单据
     *
     * @param ids 主键串
     */
    @SaCheckPermission("fms:bill:remove")
    @Log(title = "财务单据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(fmsBillService.deleteWithValidByIds(List.of(ids), true));
    }

  /**
   * 执行财务单据
   */
  @SaCheckPermission("fms:bill:exec")
  @Log(title = "财务单据执行", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping("/exec")
  public R<Void> exec(@Validated(EditGroup.class) @RequestBody FmsBill bill) {
    return fmsBillService.execBill(bill);
  }

  /**
   * 冲销财务单据
   */
  @SaCheckPermission("fms:bill:back")
  @Log(title = "财务单据冲销", businessType = BusinessType.OTHER)
  @RepeatSubmit()
  @PutMapping("/back")
  public R<Void> back(@Validated({AddGroup.class, EditGroup.class}) @RequestBody FmsBill bill) {
    return fmsBillService.backByBill(bill);
  }

  /**
   * 财务单据分期
   */
  @SaCheckPermission("fms:bill:divide")
  @Log(title = "财务单据分期", businessType = BusinessType.OTHER)
  @RepeatSubmit()
  @PostMapping("/divide")
  public R<Void> divide(@Validated({EditGroup.class}) @RequestBody FmsBill bill) {
    return fmsBillService.divide(bill);
  }
}
